<script setup lang="ts">
import type { FormInstance } from 'element-plus'
import { ElMessage } from 'element-plus'
import type { FormType } from './index'
import { GetDataDetail, PostCreateData, PutModifyData } from '@/api/modules/postage'
import detailStore from '@/store/modules/cacheData'

const router = useRouter()
const route = useRoute()
const detailData = detailStore()

const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<FormType>({
  province: '',
  city: '',
  baseKg: 0,
  baseFee: 0,
  stepKg: 0,
  stepFee: 0,
  flag: 0,
})
const id = route.query.id as string

// 图片上传赋值
// function handleUploadSuccess(url: string, key: string) {
//   if (url) {
//     // eslint-disable-next-line ts/ban-ts-comment
//     // @ts-expect-error
//     ruleForm.value[key] = url
//   }
// }

// 提交
async function SubmitData() {
  if (id) {
    // 编辑
    ruleForm.value.id = id
    PutModifyData(ruleForm.value).then((res: any) => {
      if (res.statusCode === 200) {
        ElMessage.success('编辑成功')
        router.push({ name: 'postageIndex' })
      }
      else {
        if (typeof res.errors === 'string') {
          ElMessage.error(`编辑失败。${res.errors}`)
        }
        else {
          let errorStr = ''
          for (const key in res.errors) {
            errorStr += `${key}：${res.errors[key]}；`
          }
          ElMessage.error(`编辑失败。${errorStr}`)
        }
      }
    })
  }
  else {
    // 添加
    PostCreateData(ruleForm.value).then((res: any) => {
      if (res.statusCode === 200) {
        ElMessage.success('添加成功')
        detailData.EmptyPostage()
        router.push({ name: 'postageIndex' })
      }
      else {
        if (typeof res.errors === 'string') {
          ElMessage.error(`添加失败。${res.errors}`)
        }
        else {
          let errorStr = ''
          for (const key in res.errors) {
            errorStr += `${key}：${res.errors[key]}；`
          }
          ElMessage.error(`添加失败。${errorStr}`)
        }
      }
    })
  }
}

watch(ruleForm, (newValue) => {
  if (!id) {
    detailData.RememberPostage(newValue)
  }
}, { deep: true })

onMounted(async () => {
  if (id) {
    await GetDataDetail(id).then((res: any) => {
      if (res.statusCode === 200) {
        ruleForm.value = res.data
      }
    })
  }
  else {
    ruleForm.value = detailData.GetPostageData()
  }
})
</script>

<template>
  <div>
    <PageHeader :title="id ? '编辑' : '添加'" />
    <ElForm ref="ruleFormRef" label-position="top" label-width="80px" :model="ruleForm">
      <PageMain title="基础信息">
        <ElRow :gutter="20">
          <ElCol :md="8">
            <ElFormItem label="模板类型">
              <el-select v-model="ruleForm.flag" placeholder="请选择模板类型" style="width: 300px;">
                <el-option label="通用模板" :value="1" />
                <el-option label="顺丰模板" :value="2" />
              </el-select>
            </ElFormItem>
          </ElCol>
          <ElCol :md="8">
            <ElFormItem label="地区">
              <el-input v-model="ruleForm.province" style="width: 300px;" placeholder="请输入地区" />
            </ElFormItem>
          </ElCol>
          <ElCol :md="8">
            <ElFormItem label="市">
              <el-input v-model="ruleForm.city" style="width: 300px;" placeholder="请输入市" />
            </ElFormItem>
          </ElCol>
          <ElCol :md="8">
            <ElFormItem label="首重公斤">
              <el-input v-model="ruleForm.baseKg" style="width: 300px;" placeholder="请输入首重公斤" />
            </ElFormItem>
          </ElCol>
          <ElCol :md="8">
            <ElFormItem label="首重运费">
              <el-input v-model="ruleForm.baseFee" style="width: 300px;" placeholder="请输入首重运费" />
            </ElFormItem>
          </ElCol>
          <ElCol :md="8">
            <ElFormItem label="续重公斤">
              <el-input v-model="ruleForm.stepKg" style="width: 300px;" placeholder="请输入续重公斤" />
            </ElFormItem>
          </ElCol>
          <ElCol :md="8">
            <ElFormItem label="续重公斤单价">
              <el-input v-model="ruleForm.stepFee" style="width: 300px;" placeholder="请输入续重公斤单价" />
            </ElFormItem>
          </ElCol>
        </ElRow>
      </PageMain>
    </ElForm>
    <div style="height: 80px;" />
    <FixedActionBar>
      <ElButton type="primary" size="large" @click="SubmitData()">
        提交
      </ElButton>
    </FixedActionBar>
  </div>
</template>

<style lang="scss" scoped>
.box {
  .title {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
  }
}

.card-box {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-gap: 10px;
}
</style>
